package com.auroramanito.uaa.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.auroramanito.uaa.config.AppProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;


//mooc.sms-provider.name=ali
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "auroramanito.sms-provider",name = "name",havingValue = "ali")
public class SmsServiceAliSmsImpl implements SmsService{

    private final IAcsClient client;
    private final AppProperties appProperties;
    @Override
    public void send(String mobile, String msg) {
        val request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(appProperties.getSmsProvider().getApiUrl());
        request.setSysAction("阿里云短信测试");
        request.setSysVersion("2017-05-25");
        request.putQueryParameter("RegionId","");
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("SignName","登录验证");
        request.putQueryParameter("TemplateCode","SMS_154950909");
        request.putQueryParameter("TemplateParam","{\"code\":\""
                + msg +
                "\",\"product\":\"慕课网实战Spring Security\"}");

        try {
            val commonResponse = client.getCommonResponse(request);
            log.info("短信发送结果{}",commonResponse.getData());
        } catch (ClientException e) {
            log.error("短信发送结果{}",e.getLocalizedMessage());
        }
    }
}
